package com.rmfyzxfw.caseprocess.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @description:
 * @Author: GYW  742926306@qq.com
 * @Date: 2023/5/8 10:10
 */
public class Bean2MapUtil {

    public static Map<String, Object> beanToMap(Object bean) {
        return beanToMap(bean, new Object());
    }

    public static <T> Map<String, T> beanToMap(Object bean, T valueType) {
        Map<String, T> map = new HashMap<>();
        Class<?> beanClass = bean.getClass();
        try {
            Field[] fields = beanClass.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
//                T value = (T) field.get(bean);
                Method method = beanClass.getDeclaredMethod("get"+initStr(field.getName()));
                T value = (T) method.invoke(bean);
                if (Objects.nonNull(value)) {
                    map.put(field.getName(), value);
                }
            }
        } catch (Exception e) {

        }
        return map;
    }

    public static Map<String, Object> beanToMapIgnoreProperties(Object bean, List<String> ignores) {
        return beanToMapIgnoreProperties(bean, ignores, new Object());
    }

    public static <T> Map<String, T> beanToMapIgnoreProperties(Object bean, List<String> ignores, T valueType) {
        Map<String, T> map = new HashMap<>();
        Class<?> beanClass = bean.getClass();
        try {
            Field[] fields = beanClass.getDeclaredFields();
            for (Field field : fields) {
                if(ignores.contains(field.getName())){
                    continue;
                }
                field.setAccessible(true);
                                Method method = beanClass.getDeclaredMethod("get"+initStr(field.getName()));
                T value = (T) method.invoke(bean);
                if (Objects.nonNull(value)) {
                    map.put(field.getName(), value);
                }
            }
        } catch (Exception e) {

        }
        return map;
    }

    public static  Map<String, Object> beanToMapSpecifyProperties(Object bean,  List<String> specify) {
        return beanToMapSpecifyProperties(bean, specify, new Object());
    }

    public static <T> Map<String, T> beanToMapSpecifyProperties(Object bean, List<String> specify, T valueType) {
        Map<String, T> map = new HashMap<>();
        Class<?> beanClass = bean.getClass();
        try {
            Field[] fields = beanClass.getDeclaredFields();
            for (Field field : fields) {
                if(!specify.contains(field.getName())){
                    continue;
                }
                field.setAccessible(true);
                Method method = beanClass.getDeclaredMethod("get"+initStr(field.getName()));
                T value = (T) method.invoke(bean);
                if (Objects.nonNull(value)) {
                    map.put(field.getName(), value);
                }
            }
        } catch (Exception e) {

        }
        return map;
    }

    /**
     * 将单词的首字母大写
     * @param old
     * @return
     */

    public static String initStr(String old){
        String str = old.substring(0,1).toUpperCase() + old.substring(1) ;
        return str ;
    }

    public static void main(String[] args) {
        Boolean b = Boolean.TRUE;
        System.out.println(b);
    }
}